package com.cloudkinto.service.covershows.impl;

import com.cloudkinto.entity.CustomerCaseDo;
import com.cloudkinto.dao.CustomerCaseDao;
import com.cloudkinto.entity.NewsInformationDo;
import com.cloudkinto.service.covershows.CustomerCaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import com.cloudkinto.service.covershows.vo.*;
import com.cloudkinto.service.file.FileService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 *  service实现类
 * </p>
 * @author zhangyy
 * @since 2020-10-12
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class CustomerCaseServiceImpl extends ServiceImpl<CustomerCaseDao, CustomerCaseDo> implements CustomerCaseService {
    @Autowired
    private CustomerCaseDao dao;
    @Autowired
    private FileService fileService;

    @Override
    public SingleResult addInit() {
        return SingleResult.success("");
    }

    @Override
    public void add(CustomerCaseAddReq req, Long userId) {
        CustomerCaseDo entityDo = new CustomerCaseDo();
        BeanUtils.copyProperties(req, entityDo);
        entityDo.setBrowseCount(10000);
        entityDo.setCollectCount(10000);
        StringBuilder sb = new StringBuilder("");
        if(!CollectionUtils.isEmpty(req.getAntistopList())){
            for (String s : req.getAntistopList()) {
                sb.append(s + ";");
            }
        }
        entityDo.setAntistop(sb.toString());
        handlerAddBefore(entityDo, userId);
        dao.insert(entityDo);
        fileService.uploadFile(req.getFileId());
    }

    //新增校验
    private void addCheck(Object req) {

    }

    //做新增之前要执行的业务定制处理
    private void handlerAddBefore(CustomerCaseDo entityDo, Long userId) {
        entityDo.setCreateTime(new Date());
    }

    //将新增的do转换成要返回的实体
    private Object handlerAddAfter(NewsInformationDo entityDo) {
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return res;
    }

    @Override
    public SingleResult updateInit(Long id) {
        return SingleResult.success(detail(id));
    }

    @Override
    public void update(CustomerCaseUpdateReq req, Long userId) {
        CustomerCaseDo entityDo = dao.selectById(req.getId());
        if(req.getFileId() != null){
            fileService.deleteFile(entityDo.getFileId());
            fileService.uploadFile2(req.getFileId(),entityDo.getFileId());
        }
        BeanUtils.copyProperties(req, entityDo);
        StringBuilder sb = new StringBuilder("");
        if(!CollectionUtils.isEmpty(req.getAntistopList())){
            for (String s : req.getAntistopList()) {
                sb.append(s + ";");
            }
        }

        entityDo.setAntistop(sb.toString());
        handlerUpdateBefore(entityDo, userId);
        dao.updateById(entityDo);
    }

    //编辑校验
    private void updateCheck(Object req) {

    }

    //做修改之前要执行的业务定制处理
    private void handlerUpdateBefore(CustomerCaseDo entityDo, Long userId) {
    }

    //做修改之后要执行的业务定制处理
    private Object handlerUpdateAfter(NewsInformationDo entityDo) {
        Object res = new Object();
        BeanUtils.copyProperties(entityDo, res);
        return res;
    }

    @Override
    public int delete(Long id, Long userId) {
        return dao.deleteById(id);
    }


    @Override
    public CustomerCaseDetailRes detail(Long id) {
        CustomerCaseDo entityDo =  dao.selectById(id);
        entityDo.setBrowseCount(entityDo.getBrowseCount() + 1);
        entityDo.setCollectCount(entityDo.getCollectCount() + 1);
        dao.updateById(entityDo);
        CustomerCaseDetailRes res = new CustomerCaseDetailRes();
        BeanUtils.copyProperties(entityDo, res);
        if(StringUtils.isNotBlank(entityDo.getAntistop())){
            String[] split = entityDo.getAntistop().split(";");
            res.setAntistopList(Arrays.asList(split));
        }
        res.setFileUrl(fileService.getFilePath(entityDo.getFileId()));

        return res;
    }

    //查询明细之后要执行的业务定制处理
    private Object handlerDetailAfter(Object res) {
        return res;
    }

    @Override
    public SingleResult pageInit() {
        return SingleResult.success("");
    }

    @Override
    public List<CustomerCasePageRes> getList(Map<String, String> map) {
        QueryWrapper<CustomerCaseDo> wrapper = queryBuild(map);
        List<CustomerCaseDo> list = this.list(wrapper);
        List<CustomerCasePageRes> result = handlerListAfter(list);
        return result;
    }

    @Override
    public PageResult getListPage(Map<String, String> map) {
        QueryWrapper<CustomerCaseDo> wrapper = queryBuild(map);
        IPage<CustomerCaseDo> page = pageInit(map);
        page = this.page(page, wrapper);
        List<CustomerCasePageRes> resList = handlerListAfter(page.getRecords());
        PageResult result = new PageResult(resList, (int)page.getCurrent(), (int)page.getSize(), (int)page.getTotal(), (int)page.getPages());
        return result;
    }


    //查询分页列表之后对列表数据的处理
    private List<CustomerCasePageRes> handlerListAfter(List<CustomerCaseDo> list ){
        List<CustomerCasePageRes> result = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)){
            list.forEach(newsInformationDo -> {
                CustomerCasePageRes res = new CustomerCasePageRes();
                BeanUtils.copyProperties(newsInformationDo, res);
                if(StringUtils.isNotBlank(newsInformationDo.getAntistop())){
                    String[] split = newsInformationDo.getAntistop().split(";");
                    res.setAntistopList(Arrays.asList(split));
                }
                res.setFileUrl(fileService.getFilePath(newsInformationDo.getFileId()));
                result.add(res);
            });
        }
        return result;
    }

    private IPage<CustomerCaseDo> pageInit(Map<String, String> map) {
        IPage<CustomerCaseDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 10);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

    private QueryWrapper<CustomerCaseDo> queryBuild(Map<String, String> map) {
        QueryWrapper<CustomerCaseDo> wrapper = new QueryWrapper<>();
//        if (StringUtils.isNotBlank(map.get("keyword"))) {
//            wrapper.like("keyword", map.get("keyword"));
//        }
        return wrapper.orderByDesc("id");
    }


}
